﻿
Partial Class CreditCardPage
    Inherits System.Web.UI.UserControl
    Private pMode As DetailsViewMode
    Public Property Mode() As DetailsViewMode
        Get
            Return pMode
        End Get
        Set(ByVal value As DetailsViewMode)
            pMode = value
        End Set
    End Property
    Private pShowButton As Boolean = True
    Public Property ShowButton() As Boolean
        Get
            Return pShowButton
        End Get
        Set(ByVal value As Boolean)
            pShowButton = value
        End Set
    End Property
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsNothing(Session("UserObject")) Then
            lblMyCustomerID.Text = CType(Session("UserObject"), User).CustomerID
        End If
        If IsNothing(pMode) Then
            pMode = DetailsViewMode.Insert
        End If
        dvCardInfo.ChangeMode(pMode)
        If pMode = DetailsViewMode.Edit Then
            btnSave.Text = "Update"
        ElseIf pMode = DetailsViewMode.Insert Then
            btnSave.Text = "Add"
        End If
        btnSave.Visible = pShowButton
    End Sub

    Protected Sub vldCusCardNumber_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs)
        Dim valid As Boolean = True
        Dim cardnum As String = args.Value.Trim
        Dim cardtype As String = CType(dvCardInfo.FindControl("ddlCardType"), DropDownList).SelectedValue
        If cardnum.Length > 0 Then
            Dim char1 As String = cardnum.Substring(0, 1)
            Select Case cardtype
                Case "MasterCard"
                    If Integer.Parse(char1) <> 5 OrElse cardnum.Length <> 16 Then
                        valid = False
                    End If
                Case "Visa"
                    If Integer.Parse(char1) <> 4 OrElse cardnum.Length <> 16 Then
                        valid = False
                    End If
                Case "American Express"
                    If Integer.Parse(char1) <> 3 OrElse cardnum.Length <> 15 Then
                        valid = False
                    End If
                Case "Discover"
                    If Integer.Parse(char1) <> 6 OrElse cardnum.Length <> 16 Then
                        valid = False
                    End If
            End Select
        Else
            valid = False
        End If

        args.IsValid = valid
    End Sub

    Protected Sub vldCusCVV_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs)
        Dim valid As Boolean = True
        Dim code As String = args.Value.Trim
        Dim cardtype As String = CType(dvCardInfo.FindControl("ddlCardType"), DropDownList).SelectedValue
        If code.Length > 0 Then
            Select Case cardtype
                Case "American Express"
                    If code.Length <> 4 Then
                        valid = False
                    End If
                Case Else
                    If code.Length <> 3 Then
                        valid = False
                    End If
            End Select
        Else
            valid = False
        End If
        args.IsValid = valid
    End Sub

    Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
        Save()
    End Sub

    Protected Sub dvCardInfo_ItemInserted(sender As Object, e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvCardInfo.ItemInserted
        e.KeepInInsertMode = True
        If Not e.Exception Is Nothing Then
            lblError.Text = "An error occured in the database: " & e.Exception.Message
            e.ExceptionHandled = True
        ElseIf e.AffectedRows = 0 Then
            lblError.Text = "A processing error occured, please try again."
            e.ExceptionHandled = True
        Else
            lblError.Text = String.Empty
        End If
    End Sub

    Protected Sub dvCardInfo_ItemUpdated(sender As Object, e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles dvCardInfo.ItemUpdated
        e.KeepInEditMode = True
        If Not e.Exception Is Nothing Then
            lblError.Text = "An error occured in the database: " & e.Exception.Message
            e.ExceptionHandled = True
        ElseIf e.AffectedRows = 0 Then
            lblError.Text = "A processing error occured, please try again."
            e.ExceptionHandled = True
        Else
            lblError.Text = String.Empty
        End If
    End Sub
    Public Sub Validate()
        Page.Validate()
    End Sub
    Public Sub Save()
        If Page.IsValid Then
            Dim retval As Boolean = False
            If pMode = DetailsViewMode.Edit Then
                dvCardInfo.UpdateItem(True)
            ElseIf pMode = DetailsViewMode.Insert Then
                dvCardInfo.InsertItem(True)
            End If
            If pShowButton AndAlso pMode = DetailsViewMode.Insert Then
                btnSave.Visible = False
                dvCardInfo.Visible = False
                lblError.Text = "New Card Added"
            End If
        End If
    End Sub

End Class
